//
//  Price.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import Foundation


@propertyWrapper
struct Price {
    private(set) var value: String = ""
    /// NN NNN NNN NNN T format
    var wrappedValue: String {
        get { value }
        set {
            let s = newValue
            let index = s.index(s.startIndex, offsetBy: s.count % 3)
            let sPart = s[index...]
            
            let start = s[..<index] == "" ? "" : s[..<index] + " "
            
            value = start + String(sPart.enumerated().map { $0 > 0 && $0 % 3 == 0 ? [" ", $1] : [$1]}.joined()) + " ₸"
        }
    }
    init(initialValue: String) {
        self.wrappedValue = initialValue
    }
}
@propertyWrapper
struct Range {
    private(set) var value: String = ""
    
    var wrappedValue: String {
        get { value }
        set {
            let values = newValue.split(separator: "-")
            var emptyStrKeys = ["0", "0.0"]
            
            if values.count == 2 {
                
                let otNotNil = !emptyStrKeys.contains(String(values[0]))
                let doNotNil = !emptyStrKeys.contains(String(values[1]))
                
                if otNotNil && doNotNil {
                    value = "от \(values[0]) до \(values[1])"
                }  else if !otNotNil && doNotNil {
                    value = "до \(values[1])"
                } else if !doNotNil && otNotNil {
                    value = "от \(values[0])"
                } else {
                    value = ""
                }
            }
        }
    }
    init(initialValue: String) {
        self.wrappedValue = initialValue
    }
}

